// pages/user/stujilv/stujilv.js
const request = require('../../../utils/request.js');
Page({

  data: {
    dateOptions: [],      // 下拉日期选项
    selectedDate: '',     // 当前选中的日期
    records: [],
  },

  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.findData();
  },

  findData() {
    const userInfo = wx.getStorageSync('userInfo');
    const param = {
      user_id: userInfo.id
    };
    request.post("/findThesaurusLog", param)
      .then(res => {
        const options = res.data.map(item => {
          return {
            text: `${item.date}（${item.count}条）`,
            value: item.date
          };
        });
        this.setData({
          dateOptions: options,
          selectedDate: options.length > 0 ? options[0].value : ''
        });
        if (options.length > 0) {
          this.loadRecords(options[0].value);
        }
      })
      .catch(err => {
        console.error("请求失败", err);
      });
  },

  loadRecords(date) {
    const userInfo = wx.getStorageSync('userInfo');
    request.post("/getThesaurusLogByDate", {
      user_id: userInfo.id,
      date: date
    }).then(res => {
      console.log(res)
      const records = res.data.map(item => ({
        ...item,
        showFanyi: false // 初始化时隐藏翻译
      }));
      this.setData({ records: records });
      // this.setData({ records: res.data });
    });
  },

  onDateChange(e) {
    const selected = e.detail;
    this.setData({ selectedDate: selected });
    this.loadRecords(selected);
  },
  toggleFanyi(e) {
    const index = e.currentTarget.dataset.index;
    const records = this.data.records;
    records[index].showFanyi = !records[index].showFanyi;
    this.setData({
      records: [...records]
    });
  }
  

})